/*
 * Created by JFormDesigner on Thu Jun 15 09:01:41 CST 2023
 */

package fm.type;

import dao.Dao_trade;
import dao.Dao_type;
import model.TradeType;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class UpdateType extends JInternalFrame {
    String [] columns = {"类别编号","类别名称","上限","下限"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            TradeType type = (TradeType) list.get(i);

            results[i][0] = type.getType_id();
            results[i][1] = type.getType_name();
            results[i][3] = type.getMin();
            results[i][2] = type.getMax();
        }
        return results;
    }

    public UpdateType() {
        initComponents();
        iniData();
    }

    public void iniData() {
        Object[][] results = getFS(Dao_type.selectAll());
        DefaultTableModel model = new DefaultTableModel(results, columns);
        table.setModel(model);
    }

    private void cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void tableMouseClicked(MouseEvent e) {
        // TODO add your code here
        int setRow = table.getSelectedRow();
        if(setRow == -1) return;

        String typeID = table.getValueAt(setRow,0).toString().trim();
        String typeName = table.getValueAt(setRow,1).toString().trim();
        String min = table.getValueAt(setRow,3).toString().trim();
        String max = table.getValueAt(setRow,2).toString().trim();

        typeIDTextField.setText(typeID);
        typeTextField.setText(typeName);
        minTextField.setText(min);
        maxTextField.setText(max);
    }

    private void delete(ActionEvent e) {
        // TODO add your code here
        String typeID = typeIDTextField.getText().trim();

        int i = Dao_type.deleteType(typeID);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"删除成功！");

            Object [][] results = getFS(Dao_type.selectAll());
            DefaultTableModel tableModel = new DefaultTableModel(results,columns);
            table.setModel(tableModel);

            doDefaultCloseAction();
        }
    }

    private void update(ActionEvent e) {
        // TODO add your code here
        if(typeTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入类别名称！");
            return;
        }

        if(minTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入商品下限！");
            return;
        }

        int rap = Integer.parseInt(minTextField.getText().trim());
        if(rap <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的下限！");
            return;
        }

        if(maxTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入上限！");
            return;
        }

        int ycy = Integer.parseInt(maxTextField.getText().trim());
        if(ycy <= 0)
        {
            JOptionPane.showMessageDialog(null,"请输入正确的上限！");
            return;
        }

        if(ycy <= rap)
        {
            JOptionPane.showMessageDialog(null,"上限小于下限！！");
            return;
        }

        String typeID = typeIDTextField.getText().trim();
        String type = typeTextField.getText().trim();
        String min = minTextField.getText().trim();
        String max = maxTextField.getText().trim();

        int i = Dao_type.updateType(typeID, type, Integer.parseInt(max), Integer.parseInt(min));

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"修改成功！");

            Object [][] results = getFS(Dao_type.selectAll());
            DefaultTableModel tableModel = new DefaultTableModel(results,columns);
            table.setModel(tableModel);

            doDefaultCloseAction();
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        updateButton = new JButton();
        deleteButton = new JButton();
        cancelButton = new JButton();
        panel2 = new JPanel();
        scrollPane1 = new JScrollPane();
        table = new JTable();
        panel3 = new JPanel();
        label1 = new JLabel();
        typeIDTextField = new JTextField();
        label2 = new JLabel();
        maxTextField = new JTextField();
        label3 = new JLabel();
        typeTextField = new JTextField();
        label4 = new JLabel();
        minTextField = new JTextField();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u4fee\u6539\u7c7b\u522b");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(327, 40));
            panel1.setLayout(null);

            //---- updateButton ----
            updateButton.setText("\u4fee\u6539");
            updateButton.addActionListener(e -> update(e));
            panel1.add(updateButton);
            updateButton.setBounds(new Rectangle(new Point(134, 5), updateButton.getPreferredSize()));

            //---- deleteButton ----
            deleteButton.setText("\u5220\u9664");
            deleteButton.addActionListener(e -> delete(e));
            panel1.add(deleteButton);
            deleteButton.setBounds(new Rectangle(new Point(209, 5), deleteButton.getPreferredSize()));

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(284, 5), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 334, 473, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 200));
            panel2.setBorder(new TitledBorder("\u7c7b\u522b\u4fe1\u606f"));
            panel2.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table ----
                table.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        tableMouseClicked(e);
                    }
                });
                scrollPane1.setViewportView(table);
            }
            panel2.add(scrollPane1);
            scrollPane1.setBounds(6, 20, 470, 170);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 0, 473, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setBorder(new TitledBorder("\u7c7b\u522b\u4fe1\u606f\u4fee\u6539"));
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u7c7b\u522b\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(39, 35, label1.getPreferredSize().width, 20);

            //---- typeIDTextField ----
            typeIDTextField.setEditable(false);
            typeIDTextField.setEnabled(false);
            panel3.add(typeIDTextField);
            typeIDTextField.setBounds(99, 32, 125, typeIDTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u4e0a\u9650\uff1a");
            panel3.add(label2);
            label2.setBounds(61, 82, 55, 25);
            panel3.add(maxTextField);
            maxTextField.setBounds(99, 80, 126, maxTextField.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u7c7b\u522b\u540d\u79f0\uff1a");
            panel3.add(label3);
            label3.setBounds(257, 35, label3.getPreferredSize().width, 24);
            panel3.add(typeTextField);
            typeTextField.setBounds(322, 34, 139, typeTextField.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u4e0b\u9650\uff1a");
            panel3.add(label4);
            label4.setBounds(280, 83, label4.getPreferredSize().width, 22);
            panel3.add(minTextField);
            minTextField.setBounds(322, 79, 139, minTextField.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 200, 473, 134);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton updateButton;
    private JButton deleteButton;
    private JButton cancelButton;
    private JPanel panel2;
    private JScrollPane scrollPane1;
    private JTable table;
    private JPanel panel3;
    private JLabel label1;
    private JTextField typeIDTextField;
    private JLabel label2;
    private JTextField maxTextField;
    private JLabel label3;
    private JTextField typeTextField;
    private JLabel label4;
    private JTextField minTextField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
